package com.orcsystem.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcsystem.common.to.SkuEsModel;
import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.Query;
import com.orcsystem.common.utils.R;
import com.orcsystem.product.dao.SpuInfoDao;
import com.orcsystem.product.entity.*;
import com.orcsystem.product.feign.SearchFeignService;
import com.orcsystem.product.feign.WareFeignService;
import com.orcsystem.product.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    BrandService brandService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    @Autowired
    AttrService attrService;
    @Autowired
    WareFeignService wareFeignService;
    @Autowired
    SearchFeignService searchFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void up(Long spuId) {
        // 拿到所有的sku列表
        List<SkuInfoEntity> skus = skuInfoService.getSkusBySpuId(spuId);

        // 查出这个skus 中所有 规格属性
        List<ProductAttrValueEntity> productAttrValues = productAttrValueService.baseAttrListForSpu(spuId);
        List<Long> attrIds = productAttrValues.stream().map(ProductAttrValueEntity::getAttrId).collect(Collectors.toList());

        // 所有attr 具备search_type = 1 的 attrId 集合
        List<Long> searchIds = attrService.selectSearchAttrIds(attrIds);
        Set<Long> searchAttrIds = new HashSet<>(searchIds);

        // 统一查库存
        List<Long> skuIds = skus.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());
        Map<Long, Boolean> skuHasStockMap = null;
        try{
//            Res<List<SkuHasStock>> skuHasStock = wareFeignService.getSkuHasStock(skuIds);
//            skuHasStockMap = skuHasStock.getData().stream().collect(Collectors.toMap(SkuHasStock::getSkuId,SkuHasStock::getHasStock));
        }catch (Exception e){
            log.error("库存服务查询异常:原因{}",e);
        }



        // 过滤 规格属性中包含 searchAttrIds 里的 id 的数据
        List<SkuEsModel.Attrs> attrs = productAttrValues.stream().filter(item -> searchAttrIds.contains(item.getAttrId())).map(item -> {
            SkuEsModel.Attrs attrsEntity = new SkuEsModel.Attrs();
            BeanUtils.copyProperties(item, attrsEntity);
            return attrsEntity;
        }).collect(Collectors.toList());
        List<SkuEsModel> upProducts = new ArrayList<>();

        // 封装对象
        Map<Long, Boolean> finalSkuHasStockMap = skuHasStockMap;
        List<SkuEsModel> skuEsModelList = skus.stream().map(sku -> {
            // ----- 组装数据 ------
            SkuEsModel skuEsModel = new SkuEsModel();
            // 复制 值  spuId,skuId,skuTitle,brandId,catalogId
            BeanUtils.copyProperties(sku, skuEsModel);
            // 价格
            skuEsModel.setSkuPrice(sku.getPrice());
            // 默认图片
            skuEsModel.setSkuImg(sku.getSkuDefaultImg());
            // TODO 热度评分
            skuEsModel.setHotScore(0L);
            // TODO 是否有库存 hasStock
            if (finalSkuHasStockMap == null){
                skuEsModel.setHasStock(true);
            }else {
                skuEsModel.setHasStock(finalSkuHasStockMap.get(sku.getSkuId()));
            }

            BrandEntity brand = brandService.getById(sku.getBrandId());
            // 品牌id 、名字 、图片
            skuEsModel.setBrandId(brand.getBrandId());
            skuEsModel.setBrandName(brand.getName());
            skuEsModel.setBrandImg(brand.getLogo());

            CategoryEntity categoryEntity = categoryService.getById(sku.getCatalogId());
            // 分类名
            skuEsModel.setCatalogName(categoryEntity.getName());
            // TODO list[attrs{attrId,attrName,attrValue}]
            // 规格属性
            skuEsModel.setAttrs(attrs);
            return skuEsModel;
        }).collect(Collectors.toList());
        R r = searchFeignService.productStatusUp(skuEsModelList);
        if (r.getCode() == 0){
            // 调用成功
            // TODO 修改当前spu的状态
        }else {
            // 调用失败
        }

    }

}
